package com.hzya.frame.mdm.mdmModule.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.mdm.mdmModule.service.IMdmModuleService;
import com.hzya.frame.mdm.mdmModule.vo.ImportExcelVO;
import com.hzya.frame.mdm.mdmModuleDbFileds.entity.MdmModuleDbFiledsEntity;
import com.hzya.frame.web.entity.JsonResultEntity;
import jline.internal.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/excel")
public class ImportExcelController {
    @Autowired
    private IMdmModuleService iMdmModuleService;

    /**
     * 导入模版
     */
    @RequestMapping(value = "/importTemplateFile" ,method = RequestMethod.POST)
    public JsonResultEntity importTemplateFile(@RequestParam("file") MultipartFile file,
                                               @ModelAttribute ImportExcelVO importExcelVO){

            List<MdmModuleDbFiledsEntity> entities = iMdmModuleService.importTemplateFile(file,importExcelVO);
            // 新增字段
            for (MdmModuleDbFiledsEntity entity : entities) {
                // JSONObject object = (JSONObject) JSONObject.toJSON(entity);

                String str = JSONObject.toJSONString(entity);
                Map<String,String> jsonStr=new HashMap<>();
                jsonStr.put("jsonStr",str);
                String jsonS = JSON.toJSONString(jsonStr);
                JSONObject jsonObject = JSONObject.parseObject(jsonS);

                iMdmModuleService.saveMdmDbField(jsonObject);
            }
            return new JsonResultEntity("导入模版成功",true,200);

    }
}
